#!/usr/local/bin/python
#-*- encoding:utf-8 -*-
import os 
import json
import codecs
import re
wrong = 0;
right = 0;
invalid_escape = re.compile(r'\\[0-7]{1,3}');
def replace_with_byte(match):
	return chr(int(match.group(0)[1:],8));

def repair(brokenjson):
	return invalid_escape.sub(replace_with_byte, brokenjson);

def process(file,t):
	global wrong;	
	global right;
	f = open(file, 'r');
	for line in f:
		li = line.strip().split('\t',1);
		if len(li) != 2:
			print 'Wrong: ' + str(len(li)) +':' +  line;
			continue;
		id = li[0];
		detailInfoList = [];
		try:
			detailInfoList = json.loads(repair(li[1].replace('\\r','').replace('\\n','')), strict=False)['detailInfo'];
		
		except Exception, e:
			wrong += 1;
			print 'wrong: %d, right: %d : %s ' % (wrong, right, e);
			

		if len(detailInfoList) == 0:
			pass
		else: 
			detailInfo = detailInfoList[0];
			s = '%s\t%s' %(detailInfo['name'] ,detailInfo['description']);
			
			right += 1;
			t.write('%s\t%s\n' % (id, s));

	f.close();

def main(rootDir):
	t = codecs.open('/home/wangshuxin/app_info.mp','w', 'utf-8');
	for lists in os.listdir(rootDir): 
        	path = os.path.join(rootDir, lists) 
        	print 'Processing: ' + path; 
            	process(path, t)
	t.close(); 


if __name__ == '__main__':
	rootDir='/home/wangshuxin/app_search/app_info/';
	main(rootDir)
